﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebMatrix.WebData;
using Mvc4.Filters;
using Mvc4.Models;
using System.IO;
using BussinesLayer.Services;
using DataAccess.Datatypes;
using DataModel;

namespace Mvc4.Controllers
{
    [Authorize]
    [InitializeSimpleMembership]
    public class AttributeController : Controller
    {

        GoodServices gs = new GoodServices();

        public ActionResult Create(int siteId, string siteName, int id = 0)
        {
            Good good = gs.GetGoodEdit(id, WebSecurity.CurrentUserId, siteId);
            if (good == null)
            {
                return HttpNotFound();
            }

            ViewBag.siteId = siteId;
            ViewBag.Name = siteName;
            ViewBag.SiteName = siteName + " - New Attribute";
            ViewBag.GoodName = good.Name;

            ViewBag.GoodId = id;
            ViewBag.SiteAttributeId = new SelectList(gs.GetSiteAttributes(siteId), "id", "Name");
            return View();
        }

        //
        // POST: /Attribute/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(AttributeSet att, int siteId, string siteName, int id = 0)
        {
            Good good = gs.GetGoodEdit(id, WebSecurity.CurrentUserId, siteId);
            if (good == null)
            {
                return HttpNotFound();
            }

            ViewBag.siteId = siteId;
            ViewBag.Name = siteName;
            ViewBag.SiteName = siteName + " - New Attribute";
            ViewBag.GoodName = good.Name;

            att.GoodId = att.id;
            att.id = 0;

            if (att.Value != null && att.Value != "")
            {
                gs.AddAttribute(att.GoodId, att.SiteAttributeId, att.Value);
                return RedirectToAction("MyStuff", siteName, null);
            }

            ViewBag.GoodId = id;
            ViewBag.SiteAttributeId = new SelectList(gs.GetSiteAttributes(siteId), "id", "Name");
            return View(att);
        }

    }
}